﻿@inject IHttpService _httpService
@inject IPopupService PopupService


@if (Model == null)
{
    <CnGalWebSite.Components.Progressings.ProgressingCard />
}
else
{
    <MDCard>
        <MCardTitle>
            @(Id==0? $"添加{_name}" : $"编辑{_name}")  @Model.Name
        </MCardTitle>
        <MCardText>
            Id：@Model.Id
        </MCardText>
        <MCardText>
            <MRow>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.Name" Label="名称" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.Icon" Label="图标" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MCheckbox @bind-Value="Model.Hide" Label="隐藏" TValue="bool" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MCheckbox @bind-Value="Model.HideInfoCard" Label="不在小卡片上展示" TValue="bool" />
                </MCol>
                <MCol Cols="12">
                    <MTextField @bind-Value="Model.Description" Label="描述" />
                </MCol>
                <MCol Cols="12">
                    <CnGalWebSite.Components.Checkboxes.InputEnumCheckboxList Items="Model.Types" TValue="ProjectPositionType" Label="支持的职位类型" />
                </MCol>
            </MRow>
        </MCardText>
        <MCardActions>
            <MSpacer></MSpacer>
            <MButton Color="primary"
                     Text
                     OnClick="SaveEdit">
                <MIcon Left>mdi-check</MIcon>
                保存
            </MButton>
        </MCardActions>

    </MDCard>

}



@code {
    [Parameter]
    public long Id { get; set; }
    [Parameter]
    public EventCallback OnEdited { get; set; }

    StallInformationTypeEditModel Model;

    private string _baseUrl = "api/stall/EditStallInformationType";
    private string _name = "橱窗附加信息类型";


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await Refresh();
    }

    public async Task Refresh()
    {
        try
        {
            Model = null;
            StateHasChanged();
            Model = Id == 0 ? new StallInformationTypeEditModel() : await _httpService.GetAsync<StallInformationTypeEditModel>($"{_baseUrl}?id={Id}");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, $"获取{_name}信息失败");
        }
    }

    public async Task SaveEdit()
    {
        try
        {
            var result = await _httpService.PostAsync<StallInformationTypeEditModel, Result>($"{_baseUrl}", Model);
            if (result.Success)
            {
                if (!string.IsNullOrWhiteSpace(result.Message))
                {
                    await PopupService.ToastAsync(result.Message, AlertTypes.Success);
                }
                await OnEdited.InvokeAsync(Model);
                StateHasChanged();
            }
            else
            {
                await PopupService.ToastAsync(result.Message ?? "操作失败", AlertTypes.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, Model.Id == 0 ? $"添加{_name}失败" : $"编辑{_name}信息失败");
        }

    }
}
